package activity;

import java.util.Set;

import com.android.volley.VolleyError;
import com.qzhy.female.R;
import com.umeng.socialize.utils.Log;

import Constant.Constance;
import android.app.Notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import fragment.HomeFragment;
import fragment.MessageFragment;
import fragment.MineFragment;
import fragment.PostFragment;
import model.HxStatus;
import utils.GsonUtils;
import utils.HTTPUtils;
import utils.SetUtils;
import volley.VolleyListener;

public class MainActivity extends FragmentActivity implements OnClickListener {

	private boolean isFirExit;
	private MessageReceiver mMessageReceiver;
	public static boolean isForeground;
	
	// 从Receiver接受自定义消息
		public static final String MESSAGE_RECEIVED_ACTION = "com.qzhy.female.permission.JPUSH_MESSAGE";
		public static final String KEY_TITLE = "title";
		public static final String KEY_MESSAGE = "message";
		public static final String KEY_EXTRAS = "extras";	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InitUI();
		InitJgNoti();
		InitTabHost();
	}
	
	private void InitJgNoti() {
		utils.JgUtil.initCustomPushNotificationBuilder(this, 1,
				R.layout.customer_notitfication_layout_one,
				R.drawable.icon_sq, R.drawable.icon_sq,
				Notification.FLAG_AUTO_CANCEL, Notification.DEFAULT_SOUND,
				Notification.DEFAULT_LIGHTS);// 自定义编号1的通知栏
		registerMessageReceiver();//初始化从Receiver接受自定义消息
	}
	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MESSAGE_RECEIVED_ACTION);
		registerReceiver(mMessageReceiver, filter);
	}
	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(MainActivity.this, "xxxxxx", Toast.LENGTH_SHORT).show();
			if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
				String messge = intent.getStringExtra(KEY_MESSAGE);
				String extras = intent.getStringExtra(KEY_EXTRAS);
				StringBuilder showMsg = new StringBuilder();
				showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
				if (!utils.JgUtil.isEmpty(extras)) {
					showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
				}
				Toast.makeText(context, showMsg.toString(), 1).show();
			}
		}
	}

	private void InitTabHost() {
		FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		View tab_home = getLayoutInflater().inflate(R.layout.item_host_tab_home, null);
		View tab_post = getLayoutInflater().inflate(R.layout.item_host_tab_post, null);
		View tab_message=getLayoutInflater().inflate(R.layout.item_host_tab_maps, null);
		View tab_mime = getLayoutInflater().inflate(R.layout.item_host_tab_currency, null);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		mTabHost.addTab(mTabHost.newTabSpec("index").setIndicator(tab_home), HomeFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("poi").setIndicator(tab_post), PostFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("maps").setIndicator(tab_message), MessageFragment.class, null);
		mTabHost.addTab(mTabHost.newTabSpec("mine").setIndicator(tab_mime), MineFragment.class, null);
	}
	private void InitUI() {
		
	}
	/**
	 * 上面导航的配置
	 */
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		default:
			break;
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		isForeground = true;
		JPushInterface.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		isForeground = false;
		JPushInterface.onPause(this);
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mMessageReceiver);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			if(isFirExit){
				finish();
				return true;
			}else {
				Toast.makeText(this, "再按一次返回主界面", Toast.LENGTH_SHORT).show();
				isFirExit=true;
			}
			new android.os.Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					isFirExit=false;
				}
			}, 1500);
			return true;//拦截系统返回事件处理
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
